﻿//using LDT.Tech.BatchTestTools.BLL;
//using LDT.Tech.BatchTestTools.Model;
using Ledict.ConditionExtract.BLL;
using Ledict.ConditionExtract.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


public partial class colLinkButton : Panel
    {        
        public colLinkButton()
        {
            InitializeComponent();
        }
        private struct Item
        {
            public string _text;
            public string _value;
            public int _right;
            public int _left;
        }
        protected override void OnPaint(PaintEventArgs pe)
        {
            this.BorderStyle = BorderStyle.None;
            base.OnPaint(pe);
        }

        public delegate void ControlLinkButtonClick(string value);
        [Description("单击链接按钮事件，返回所点击按钮的value"), Category("行为")]
        public event ControlLinkButtonClick LinkBtnClicked;

        private List<Item> _itemlist = new List<Item>();

        private string _selectValue;
        public string SelectValue
        {
            get { return _selectValue; }
            set { _selectValue = value; }
        }
        private string _selectText;
        public string SelectText
        {
            get { return _selectText; }
            set { _selectText = value; }
        }
        private LinkLabel _lastLinkLable = null;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="text"></param>
        /// <param name="value"></param>
        public void AddItem(string text,string value)
        {
            Item item = new Item();
            item._text = text;
            item._value = value;

            LinkLabel ll = new LinkLabel();
            ll.Text = string.Format("/{0}", item._text);
            //或者字符串的像素尺寸
            Graphics graphics = CreateGraphics();
            SizeF sizeF = graphics.MeasureString(ll.Text, this.Font);
            ll.Size = new Size((int)(Math.Ceiling(sizeF.Width)), (int)(Math.Ceiling(sizeF.Height)));
            //计算控件要显示的位置
            if (_itemlist.Count == 0)
            {
                item._left = 0;
                item._right = ll.Size.Width;
            }
            else
            {
                item._left = _itemlist[_itemlist.Count - 1]._right;
                item._right = item._left + ll.Size.Width;
            }
            //ll.LinkBehavior = LinkBehavior.HoverUnderline;
            ll.Location = new Point(item._left, (Size.Height - ll.Size.Height) / 2);
            ll.LinkClicked += new LinkLabelLinkClickedEventHandler(this.colLinkButton_Click);
            this.Controls.Add(ll);
            _itemlist.Add(item);

        
        }

    public void FindCCID(string ccId)
    {
        SYS_CaseCause scc = new CaseCauseBLL().GetCaseCauseByCCID(ccId);
        foreach (var item in this.Controls)
        {
            if (((LinkLabel)item).Text=="/"+scc.CC_Name)
            {
                ((LinkLabel)item).BackColor = Color.Red;
            }
        }
    }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ccId"></param>
    public void AddElement(string ccId)
        {
            List<SYS_CaseCause> list = new List<SYS_CaseCause>();
            FindElement(ccId, ref list);
            for(int i=list.Count-1;i>-1;i--)
            {
                AddItem(list[i].CC_Name, list[i].CC_ID);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ccId"></param>
        /// <param name="list"></param>
        private void FindElement(string ccId,ref List<SYS_CaseCause> list)
        {
        SYS_CaseCause scc = new CaseCauseBLL().GetCaseCauseByCCID(ccId);
        if (scc != null && Convert.ToInt32(scc.CC_NodeType) != 2)
        {
            list.Add(scc);
            FindElement(scc.CC_ParentID, ref list);
        }
    }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void colLinkButton_Click(object sender, EventArgs e)
        {
            if (_lastLinkLable != null) _lastLinkLable.BackColor = Color.Transparent;
            _lastLinkLable = sender as LinkLabel;
            _lastLinkLable.BackColor = Color.Red;
            string text = _lastLinkLable.Text.Replace("/","");
            foreach(var item in _itemlist)
            {
                if (item._text == text && LinkBtnClicked != null)
                {
                    _selectValue = item._value;
                    _selectText = item._text;
                    LinkBtnClicked(item._value);
                }  
            }
        }
    }

